package cn.ffcs.contract.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.contract.model.ContractInfo;
import cn.ffcs.contract.model.ContractUpload;
import cn.ffcs.contract.model.VContractInfo;

/**
 * 
 * 合同DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class ContractInfoDAO<D extends ContractInfo> extends BaseDAO<D> implements
        IContractInfoDAO<D>
{
	private JdbcTemplate jdbcTemplate;
    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	/**
     * 获取实体类
     * @param b
     * @return
     */
    public String getEntityName()
    {
        return new ContractInfo().getClass().getName();
    }
    

    /**
     * 按条件查询翻页
     * @param cond
     * @param currPage
     * @param pageSize
     * @return
     */
    @SuppressWarnings("unchecked")
    public PageControl findByPage(Criterion cond,int currPage, int pageSize)
    {
      
        PageControl pageControl=new PageControl(findBy(cond).size(),currPage,pageSize);

        final int startRow = pageControl.getStart();
        final int endRow=pageControl.getPageSize();
        final Criterion conds=cond;
        
        List<D> results = getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session
                                .createCriteria(getEntityName());
                        // 设置查询条件
                        Criterion criterion = Restrictions.gt("end", getCurrSysdate ());
                        if (conds!=null) criterion=Restrictions.and(criterion, conds);
                        criteria.add(criterion);
                        // 设置翻页数据
                        criteria.setFirstResult(startRow);
                        criteria.setMaxResults(endRow);

                        //按创建时间
                        criteria.addOrder(Order.desc("maturityTime"));
                        
                        return criteria.list();
                    }
                });
        ;
        PageControl control=new PageControl(results, startRow, pageSize, currPage,
                pageControl.getTotalPage(), pageControl.getTotal());
        if(pageControl.getTotal()<=pageSize*currPage){
        	control.setUseNextPage(false);
        }else {
        	control.setUseNextPage(true);
        }
        return control;
    }
    
    @SuppressWarnings("unchecked")
	public PageControl findByPageView(Criterion cond,int currPage, int pageSize,final String orderBy)
    {
      
        PageControl pageControl=new PageControl(findByView(cond).size(),currPage,pageSize);

        final int startRow = pageControl.getStart();
        final int endRow=pageControl.getPageSize();
        final Criterion conds=cond;
        
        List<D> results = getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session
                                .createCriteria(new VContractInfo().getClass().getName());
                        // 设置查询条件
                        Criterion criterion = Restrictions.gt("end", getCurrSysdate ());
                        if (conds!=null) criterion=Restrictions.and(criterion, conds);
                        criteria.add(criterion);
                        // 设置翻页数据
                        criteria.setFirstResult(startRow);
                        criteria.setMaxResults(endRow);
                        if(orderBy!=null&&!"".equals(orderBy)){
                        	if("submitTime".equalsIgnoreCase(orderBy)){
                        		criteria.addOrder(Order.desc(orderBy));
                        	}else{
                        		criteria.addOrder(Order.asc(orderBy));
                        	}
                        }
                        return criteria.list();
                    }
                });
        ;
        PageControl control=new PageControl(results, startRow, pageSize, currPage,
                pageControl.getTotalPage(), pageControl.getTotal());
        //control.setTotal(results.size());
        //control.setTotalPage(results.size()/pageSize+1);
        if(pageControl.getTotal()<=pageSize*currPage){
        	control.setUseNextPage(false);
        }else {
        	control.setUseNextPage(true);
        }
        return control;
    }
    @SuppressWarnings("unchecked")
	public List findBy(Criterion cond)
    {
        final Criterion conds=cond;
        return getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session
                                .createCriteria( getEntityName());
                       
                        //new VContractInfo().getClass().getName()
                        // 设置查询条件,查询有效数据
                        Criterion criterion = Restrictions.gt("end", getCurrSysdate());
                        if (conds!=null) criterion=Restrictions.and(criterion, conds);
                        criteria.add(criterion);

                        //按创建时间
                        criteria.addOrder(Order.desc("begin"));
                        
                        return criteria.list();
                    }
                });
    }
    @SuppressWarnings("unchecked")
	public List findByView(Criterion cond)
    {
        final Criterion conds=cond;
        return getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session
                                .createCriteria(new VContractInfo().getClass().getName());
                        // 设置查询条件,查询有效数据
                        Criterion criterion = Restrictions.gt("end", getCurrSysdate());
                        if (conds!=null) criterion=Restrictions.and(criterion, conds);
                        criteria.add(criterion);

                        //按创建时间
                        criteria.addOrder(Order.desc("begin"));
                        
                        return criteria.list();
                    }
                });
    }
    
    
    public ContractUpload getContractUpload(String id){
    	ContractUpload entity=(ContractUpload) getHibernateTemplate().get(ContractUpload.class, new Integer(id));
    	return entity;
    }
    /**
     * 删除附件表
     * @param id
     */
    public void deleteContractUpload(String id){
    	ContractUpload entity=getContractUpload(id);
    	getHibernateTemplate().delete(entity);
    }
    /**
     * 根据合同id取附件集合
     * @param contractInfoId
     * @return
     */
	@SuppressWarnings("unchecked")
	public List<ContractUpload> listContractUpload(String contractInfoId){
    	List<ContractUpload> uploadList=new ArrayList<ContractUpload>();
    	String sql=" from ContractUpload where contractInfoId='"+contractInfoId+"'";
    	uploadList=(List<ContractUpload>)getHibernateTemplate().find(sql);
    	return uploadList;
    }
    /**
     * 判断此用户是否为合同管理员
     * @param userId
     * @return
     */
    public boolean isManage(String userId){
    	String sql="select count(t.id) from t_secu_role_rela_user t where t.end>sysdate and t.secu_user='"+userId+"' and t.secu_role=5";
    	int i=this.getJdbcTemplate().queryForInt(sql);
    	if(i>0){
    		return true;
    	}else {
    		return false;
    	}
    }
    
}




